import ycoa.data.classes.ClassFactory

def cli = new CliBuilder(usage:'load')
cli.f(longOpt:'file', argName:'XML file', args:1, required:true,  'file to load')
cli.F(longOpt:'first', argName:'first record', args:1, required:false,  'first record')
cli.L(longOpt:'last', argName:'last record', args:1, required:false,  'last record')
cli.o(longOpt:'output', argName:'output file', args:1, required:false,  'output file')
cli.h(longOpt:'help',                   , args:0, required:false, 'display usaage')
def opt = cli.parse(args)
if(!opt) { return }

def data = new File(opt.file).text
def classFactory = ClassFactory.getInstance()
def first = null
if(opt.first) first = Integer.parseInt(opt.first)
def last = null
if(opt.last) last = Integer.parseInt(opt.last)
def classList = classFactory.parseXml(data, first, last)
def outputFile = null
if(opt.output) outputFile = new File(opt.output)
if(opt.output) outputFile.write("")
if(outputFile) outputFile << "<root>"
classList.each() { cls ->
  try {
    println cls.toString()
    println cls.toXmlString()
    if(outputFile) outputFile << cls.toXmlString()
  } catch(e) {
    println "RECORDS: ${first} - ${last}"
    e.printStackTrace()
    System.exit(1)
  }
}
if(outputFile) outputFile << "</root>"

